package com.winning.util.excelutil.common.controller;

import com.winning.util.excelutil.common.dto.BaseProtocolOut;
import com.winning.util.excelutil.common.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;

import java.io.Serializable;

/**
 * @description:  controller 基类
 * @author: caopengfei
 * @create: 2018-07-13 23:07
 **/
@Slf4j
public abstract class BaseController implements Serializable {

    /**
     * Description:        基础返回格式
     * ModifiedBy:
     * ModifiedTime:
     */
    public BaseProtocolOut successResult(String message, Object data) {
        return BaseProtocolOut.success(message,data);
    }

    public BaseProtocolOut success(String message) {
        return BaseProtocolOut.success(message);
    }

    public BaseProtocolOut error(int code,String message) {
        return BaseProtocolOut.error(code,message);
    }

    public BaseProtocolOut error(String message) {
        return BaseProtocolOut.error(message);
    }

    protected void checkValid(BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw  new BaseException(bindingResult.getFieldError().getDefaultMessage(), HttpStatus.BAD_REQUEST.value());
        }
    }
    public BaseProtocolOut successObjectResult(String message,Object data) {
        return BaseProtocolOut.success(message,data);
    }

    public static BaseProtocolOut tokenError(String repMsg) {
        return BaseProtocolOut.error(401,repMsg);
    }


}
